<!--suppress ALL -->
<include file="./Template/header.html" />
<link rel="stylesheet" type="text/css" href="/Template/css/updateInfo.css?v=7">
<script type="text/javascript">

	$(function() {

		//初始化页面配置
        if(isNullOrEmpty('{$employee.update_time}') && '{$employee.granted}' != '1'){
            initPage("员工注册");
		}else{
            initPage("个人资料");
		}
        if('{$employee.granted}' == '1') {
            $(".save-btn").hide();
        }

		var app = new Vue({
			el: '#container',
			data: {
				info: [],
				enterprise: [],
			},
			methods: {
				index: indexPage,
                returnHome: function() {
                    if('{$employee.granted}' != '1'){
                        window.location.href = "/wechat/home";
					}else{
                        window.location.href = "/wechat/index/bfo";
					}
                },
				updateInfo: function() {
                    if(getNowFormatDate() == '{$employee.update_time}'){
						alert("每天只能提交一次注册或修改申请");
						return;
					}
					if('{$employee.granted}' == '1'){
                        if (!confirm("变更信息后，需要企业管理员审核后才能办理业务，\n是否继续？")) {
                            return false;
                        }
					}

                    var enterpriseCode = app.enterprise.enterprise_code;
                    var adminName = $("input[name=adminName]").val();
                    var enterpriseName = $("input[name=enterpriseName]").val();
					var mobile = $("input[name=mobile]").val();
//                    var username = $("input[name=username]").val();
//                    var password = $("input[name=password]").val();
                    var note = $("textarea[name=note]").val();

                    note = note.replace(/\r\n/g,"。")
                    note = note.replace(/\n/g,"。");

                    var role = "";
                    $('input[type="checkbox"][name="role_id"]:checked').each(function () {
                        if(role == ""){
                            role = $(this).val();
                        }else{
                            role += "," + $(this).val();
                        }
                    });

                    if(isNullOrEmpty(enterpriseName)) {
                        alert("请输入企业代码并查询");
                        return false;
                    }

					if(isNullOrEmpty(adminName)) {
						alert("请填写姓名");
						return false;
					}

					if(isNullOrEmpty(mobile)) {
						alert("请填写手机号码");
						return false;
					}

					if(!checkPhone(mobile)) {
						alert("手机号码位数不对");
						return false;
					}

//                    if(isNullOrEmpty(username)) {
//                        alert("请填写用户名");
//                        return false;
//                    }
//
//                    if(password.length < 6) {
//                        alert("请填写大于6位数的密码");
//                        return false;
//                    }
                    $(".save-btn").hide();
					$.ajax({
						url:"/wechat/userCenter/updateEmployeeInfo",
						type:"post",
						data:{
						    employeeId: '{$customer.customer_id}',
                            enterpriseCode: enterpriseCode,
                            enterpriseName: enterpriseName,
                            adminName: adminName,
							mobile: mobile,
//                            username: username,
//                            password: password,
                            note: note,
                            role: role,
						},
						dataType:"JSON",
						beforeSend:function(){
							$("#loadingToast").show();
						},
						complete:function(){
							$('#loadingToast').hide();
                            window.location.href = "/wechat/home";
						},
						success:function(data){
//                            alert(data.code);
							if(data.code == 200) {
								alert("提交成功");
								window.location.href = "/wechat/home";
							}
						},
					})

				}
			}
		})

        function getEnterprise(code) {
            $.ajax({
                url:"/wechat/userCenter/getEnterprise",
                type:"post",
                data:{
                    code: code,
                },
                dataType:"JSON",
                beforeSend:function(){
                    $("#loadingToast").show();
                },
                complete:function(){
                    $('#loadingToast').hide();
                },
                success:function(data){
                    if(isNullOrEmpty(data)){
                        alert("无此通行证号码，\n请校验后与企业管理员沟通。");
                        return;
                    }
                    app.enterprise = data;
                    setTimeout(function() {
                        $(".ui-item.measure-info img").bind("click", function() {
                            var upUrl = "/Template/images/icon-up.png";
                            var downUrl = "/Template/images/icon-down.png";
                            $(this).parent().next().toggle();
                            if($(this).attr("src") == upUrl) {
                                $(this).attr("src", downUrl);
                            } else {
                                $(this).attr("src", upUrl);
                            }
                        });
                        $(".ui-item.measure-info img").each(function() {
                            $(this).trigger("click");
                        });
                    },200);
                    $("input[name=enterpriseName]").val(data.enterprise_name);
                    $("input[name=code]").val(data.enterprise_name);
                    //动态添加checkbox
					var roleIdArr = data.role_list.split(",");
                    var roleNameArr = data.role_name.split(",");

                    for(var i=0 ;i < roleIdArr.length; i++){
                        if( i %4 == 3){
                            $("#apartmentCheck").append("<br/>");
						}
                        $("#apartmentCheck").append("<label class='checkbox inline'><input type='checkbox' value='"+roleIdArr[i]+"' name='role_id'/>"+roleNameArr[i]+"&nbsp&nbsp&nbsp&nbsp</label>");
                    }
//                    $("#apartmentCheck").show();
                },
            })
        }

//        if("{$employee.granted}" == "1"){
//            $("#apartmentCheck").show();
//		}

        $("input[name=code]").change(function() {
            var code = $("input[name=code]").val();
            getEnterprise(code);
        });

        $("img[name=searchImg]").click(function() {

            var code = $("input[name=code]").val();
            getEnterprise(code);

        });

		$(".item select").change(function() {
			var value = $(this).find("option:selected").text();
			$(this).prev().val(value);
		});

		$("input[type=date]").change(function() {
			var value = $(this).val();
			$(this).prev().val(value);
		});
        app.enterprise.enterprise_code = "{$enterprise.enterprise_code}";

        if("{$employee.granted}" == "1"){
            $("#noteDiv").hide();
            $("#enterCode").hide();
		}else{
            $("#enterName").hide();
		}


	});


</script>
<!--div class="hint-info">
	根据药监局追溯要求,为了您的合法权益,请务必填写真实信息。
</div-->
<div class="edit-content">
	<!--div class = "search">
		<div class="keyword-search">
			<span class="keyword-input">企业代码：</span>
			<img name="searchImg" class="icon-keyword" src="/Template/images/icon-search.png" alt="">
			<input name="code" class="keyword-line" type="text" placeholder="请输入企业代码">
		</div>
	</div-->
	<if condition="$employee['granted'] neq '1' && isNullOrEmpty($employee['update_time'])">
		<div style="background-color: white" class="hint-info">
			您目前没有权限访问BFMIS渠道订单,请与贵公司管理员联系获取通行证号码，并提交注册信息。
		</div>
	</if>
	<if condition="$employee['granted'] neq '1' && !isNullOrEmpty($employee['update_time'])">
		<div style="background-color: white" class="hint-info">
			您已提交注册申请，请等待管理员审批申请。<br/>提交日期：{$employee.update_time}
		</div>
	</if>
	<div class="item" id="enterName">
		<span>企业通行证</span>
		<input style="width: 45%" type="text" value="{$enterprise.enterprise_name}" readonly>
	</div>
	<div class="item" id="enterCode">
		<span>企业通行证号</span>
		<input style="display: none" type="text" name="enterpriseName" value="{$enterprise.enterprise_name}" readonly>
		<input  style="width: 45%" type="text" name="code" value="{$enterprise.enterprise_name}" placeholder="请填写通行证号码">
		<img class="icon-right" src="/Template/images/icon-right.png" alt="">
	</div>
	<div class="item">
		<span>姓名</span>
		<input type="text" name="adminName" value="{$employee.admin_name}" placeholder="请填写您的真实姓名">
		<img class="icon-right" src="/Template/images/icon-right.png" alt="">
	</div>
	<div class="item">
		<span>手机号</span>
		<input type="text" name="mobile" value="{$employee.mobile}" placeholder="请填写您的手机号码">
		<img class="icon-right" src="/Template/images/icon-right.png" alt="">
	</div>
	<!--div class="item">
		<span>医生</span>
		<input type="text" name="doctorName" value="{$info.doctor_name}" placeholder="请填写您的医师姓名">
		<img class="icon-right" src="/Template/images/icon-right.png" alt="">
	</div-->
	<!--div class="item" style="border-bottom:0px">
		<span class="itemTitle" style="margin-left: 10%">—— 用于ERP系统登录的用户名密码 ——</span><br/>
	</div>
	<div class="item" style="border-bottom:0px">
		<span>用户名</span>
		<input type="text" name="username" value="{$info.username}" placeholder="请填写您的用户名">
	</div>
	<div class="item">
		<span>密码</span>
		<input type="password"  name="password" value="{$info.password}" placeholder="请填写您的密码">
	</div-->
	<div style="height: auto;" class="item">
		<span>部门角色：</span>
		<div id="apartmentCheck">
		</div>
		<span v-if="{$employee.granted} == 1">{$roleName}</span>
	</div>
	<!--div style="height: 12rem;" class="item" id="apartmentCheck">
		<span>部门角色：</span>
		<php>
			$enterpriseRoleList = explode(',', $enterprise["role_list"]);
			$apartmentArr = array();
			foreach($allRoleList as $role) {
				if (in_array($role["id"], $enterpriseRoleList)) {
					array_push($apartmentArr, $role);
				}
			}

			if ($employee["granted"] == 1) {
				$apartmentid=explode(',', $employee["role"]);
			}else{
				$apartmentid=explode(',', $employee["pre_role"]);
			}
			$lineNum = 0;
		</php>
		<br>
		<foreach name="apartmentArr" item="vo">
			<label class="checkbox inline">
				<php>$apartment_checked=in_array($vo['id'],$apartmentid)?"checked":"";</php>
				<input value="{$vo.id}" type="checkbox" name="role_id" {$apartment_checked}>{$vo.name}&nbsp&nbsp&nbsp&nbsp
				<php>$lineNum++;
					if($lineNum == 3){
						$lineNum = 0;
						echo("<br>");
					}
				</php>
			</label>
		</foreach>
	</div-->
	<div style="height: 15rem" class="item" id="noteDiv">
		<span>备注：</span>
		<textarea class="t-area" name="note" maxlength="80" placeholder="请填写">{$employee.note}</textarea>
	</div>
</div>

<div class="save-btn" v-on:click="updateInfo">
	提交
</div>
<div class="cancel-btn" v-on:click="returnHome">
	返回
</div>

<include file="./Template/footer.html" />

